use crate::{Icon, Message, Tab, theme::Theme};
use iced::{Column, Container, Element, Radio, Text};
use iced_aw::{TabLabel};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TabBarPosition {
    Top,
    Bottom,
}

impl TabBarPosition {
    pub const ALL: [TabBarPosition; 2] = [
        TabBarPosition::Top,
        TabBarPosition::Bottom,
    ];
}

impl From<TabBarPosition> for String {
    fn from(position: TabBarPosition) -> Self {
        String::from(match position {
            TabBarPosition::Top => "Top",
            TabBarPosition::Bottom => "Bottom",
        })
    }
}

impl Default for TabBarPosition {
    fn default() -> Self {
        TabBarPosition::Top
    }
}

//#[derive(Debug, Clone, Copy, PartialEq, Eq)]


pub struct TabSettings {
    pub tab_bar_position: Option<TabBarPosition>,
    pub tab_bar_theme: Option<Theme>,
}

impl TabSettings {
    pub fn new() -> Self {
        TabSettings {
            tab_bar_position: Some(TabBarPosition::Top),
            tab_bar_theme: Some(Theme::default()),
        }
    }
}

#[derive(Debug, Clone)]
pub enum SettingsMessage {
    PositionSelected(TabBarPosition),
    ThemeSelected(Theme),
}

pub struct SettingsTab {
    settings: TabSettings,
}

impl SettingsTab {
    pub fn new() -> Self {
        SettingsTab {
            settings: TabSettings::new(),
        }
    }

    pub fn settings(&mut self) -> &mut TabSettings {
        &mut self.settings
    }

    pub fn update(&mut self, message: SettingsMessage) {
        match message {
            SettingsMessage::PositionSelected(position) => {
                self.settings().tab_bar_position = Some(position)
            }
            SettingsMessage::ThemeSelected(theme) => {
                self.settings().tab_bar_theme = Some(theme)
            }
        }
    }
}

impl Tab for SettingsTab {
    type Message = Message;

    fn title(&self) -> String {
        String::from("Settings")
    }

    fn tab_label(&self) -> TabLabel {
        //TabLabel::Text(self.title())
        TabLabel::IconText(Icon::CogAlt.into(), self.title())
    }

    fn content(&mut self) -> Element<'_, Self::Message> {
        let content: Element<'_, SettingsMessage> = Container::new(
            Column::new()
                .push(
                    Text::new("TabBar position:")
                        .size(20)
                )
                .push(
                    TabBarPosition::ALL.iter().cloned().fold(
                        Column::new().padding(10).spacing(10),
                        |column, position| {
                            column.push(
                                Radio::new(
                                    position,
                                    position,
                                    self.settings().tab_bar_position,
                                    SettingsMessage::PositionSelected,
                                )
                                .size(16)
                            )
                        }
                    )
                )
                .push(
                    Text::new("TabBar color:")
                        .size(20)
                )
                .push(
                    Theme::ALL.iter().cloned().fold(
                        Column::new().padding(10).spacing(10),
                        |column, theme| {
                            column.push(
                                Radio::new(
                                    theme,
                                    theme,
                                    self.settings().tab_bar_theme,
                                    SettingsMessage::ThemeSelected,
                                )
                                .size(16)
                            )
                        }
                    )
                )
        )
        .into();

        content.map(Message::Settings)
    }
}